namespace Billing2
{
    using AjaxControlToolkit;
    using Billing2.Controls;
    using Billing2.Controls.CR;
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public class ChangeRequest : Page
    {
        private DataTable dt;
        private DataTable dtAttachmentRequestor;
        protected UpdatePanel updatePanel5;
        protected ucHighlight ucHighlight1;
        protected ucPageTitle ucPageTitle1;
        protected UpdatePanel updatePanel3;
        protected Label lblCreatedDateTimeLabel;
        protected Label lblCreatedDateTime;
        protected Label lblCRNO;
        protected Label lblCreatedByLabel;
        protected Label lblCreatedBy;
        protected Label lblCRStatus;
        protected UpdatePanel updatePanelHeader;
        protected Button btnFind;
        protected UpdatePanel updatePanelSubscriber;
        protected Label lblNewSubscriber;
        protected Label lblOldSubscriber;
        protected Label lblOrg;
        protected DropDownList ddlNewOrg;
        protected Label lblOldOrg;
        protected Label lblSite;
        protected DropDownList ddlNewSite;
        protected Label lblOldSite;
        protected Label lblCC;
        protected DropDownList ddlNewCC;
        protected Label lblOldCC;
        protected Label lblEmail;
        protected TextBox txtNewEmail;
        protected Label lblOldEmail;
        protected Label lblOldUsername;
        protected Label lblOldServiceType;
        protected Label lblOldAssetID;
        //protected Label lblOldHostname;
        protected Label lblUsername;
        protected Label lblServiceType;
        protected Label lblAssetID;
        //protected Label lblHostname;
        protected TextBox txtNewUsername;
        protected DropDownList ddlNewServiceType;
        protected TextBox txtNewAssetID;
        //protected TextBox txtNewHostname;
        protected Label lblEffDate;
        protected TextBox txtNewEffDate;
        protected CalendarExtender txtNewEffDate_CalendarExtender;
        protected Label lblOldEffDate;
        protected Label lblTermDate;
        protected TextBox txtNewTermDate;
        protected CalendarExtender txtNewTermDate_CalendarExtender;
        protected Label lblOldTermDate;
        protected Label lblStatus;
        protected RadioButtonList rblNewStatus;
        protected RadioButtonList rblOldStatus;
        protected Panel PanelRequestor;
        protected TextBox txtRequestorComment;
        protected Label Label33;
        protected ucCRAttachment ucCRAttachmentRequestor;
        protected UpdatePanel UpdatePanel1;
        protected Button BtnSubmit;
        protected ConfirmButtonExtender BtnSubmit_ConfirmButtonExtender;
        protected UpdateProgress updateprogress1;
        protected UpdateProgress updateprogress2;
        protected HiddenField xChangeRequestID;
        protected HiddenField xChangeRequestType;
        protected HiddenField xChangeRequestStatus;
        protected UpdatePanel UpdatePanel4;
        protected ucFinder ucFinder1;

        public bool Read0
        {
            get
            {
                return Convert.ToBoolean(this.ViewState["xRead0"]);
            }
            set
            {
                this.ViewState["xRead0"] = value;
            }
        }

        public bool Create0
        {
            get
            {
                return Convert.ToBoolean(this.ViewState["xCreate0"]);
            }
            set
            {
                this.ViewState["xCreate0"] = value;
            }
        }

        public bool Update0
        {
            get
            {
                return Convert.ToBoolean(this.ViewState["xUpdate0"]);
            }
            set
            {
                this.ViewState["xUpdate0"] = value;
            }
        }

        public bool Delete0
        {
            get
            {
                return Convert.ToBoolean(this.ViewState["xDelete0"]);
            }
            set
            {
                this.ViewState["xDelete0"] = value;
            }
        }

        public string xSubscriberID
        {
            get
            {
                if (this.ViewState["xSubscriberID"] == null)
                    this.ViewState["xSubscriberID"] = "0";
                return this.ViewState["xSubscriberID"].ToString();
            }
            set
            {
                this.ViewState["xSubscriberID"] = value;
            }
        }

        public string NewOrganizationID
        {
            get
            {
                if (this.ddlNewOrg.SelectedIndex == -1)
                    return "0";
                else
                    return this.ddlNewOrg.SelectedValue;
            }
            set
            {
                this.ddlNewOrg.SelectedIndex = -1;
                if (value != "" && value != "0")
                    this.ddlNewOrg.Items.FindByValue(value).Selected = true;
                this.BindNewSite();
            }
        }

        public string NewSiteID
        {
            get
            {
                if (this.ddlNewSite.SelectedIndex == -1)
                    return "0";
                else
                    return this.ddlNewSite.SelectedValue;
            }
            set
            {
                this.ddlNewSite.ClearSelection();
                if (value != "" && value != "0")
                    this.ddlNewSite.SelectedIndex = this.ddlNewSite.Items.IndexOf(this.ddlNewSite.Items.FindByValue(value));
                this.BindNewCostCentre();
            }
        }

        public string NewCostCentreID
        {
            get
            {
                if (this.ddlNewCC.SelectedIndex == -1)
                    return "0";
                else
                    return this.ddlNewCC.SelectedValue;
            }
            set
            {
                this.ddlNewCC.SelectedIndex = -1;
                if (!(value != "") || !(value != "0"))
                    return;
                this.ddlNewCC.SelectedIndex = this.ddlNewCC.Items.IndexOf(this.ddlNewCC.Items.FindByValue(value));
            }
        }

        public string ChangeRequestNo
        {
            get
            {
                return this.lblCRNO.Text;
            }
            set
            {
                this.lblCRNO.Text = Common.EscapeInteger(value);
            }
        }

        private ClsChangeRequestHelper.ChangeRequestType ChangeRequestType
        {
            get
            {
                return (ClsChangeRequestHelper.ChangeRequestType)int.Parse(this.xChangeRequestType.Value);
            }
            set
            {
                this.xChangeRequestType.Value = Microsoft.Security.Application.Encoder.HtmlEncode(((Int64)value).ToString());
                switch (value)
                {
                    case ClsChangeRequestHelper.ChangeRequestType.Create:
                        {
                            this.ucPageTitle1.PageName = ClsChangeRequestHelper.Create;
                            this.updatePanelSubscriber.Visible = true;
                            this.SetNewInfoVisble(true);
                            this.SetOldInfoVisble(false);
                            this.btnFind.Visible = false;
                            return;
                        }
                    case ClsChangeRequestHelper.ChangeRequestType.Update:
                        {
                            this.ucPageTitle1.PageName = ClsChangeRequestHelper.Update;
                            this.updatePanelSubscriber.Visible = true;
                            this.SetNewInfoVisble(true);
                            this.SetOldInfoVisble(true);
                            this.btnFind.Visible = true;


                            if (this.ChangeRequestStatus == ClsChangeRequestHelper.ChangeRequestStatus.New)
                            {
                                this.ddlNewOrg.Enabled = false;
                                this.ddlNewSite.Enabled = false;
                                this.ddlNewCC.Enabled = false;
                                this.txtNewEmail.Enabled = true;
                                this.txtNewUsername.Enabled = true;
                                this.ddlNewServiceType.Enabled = true;
                                this.txtNewAssetID.Enabled = true;
                                //this.txtNewHostname.Enabled = true;
                                this.txtNewEffDate.Enabled = true;
                                this.txtNewTermDate.Enabled = false;
                                this.rblNewStatus.Enabled = true;
                            }
                            return;
                        }
                    case ClsChangeRequestHelper.ChangeRequestType.Delete:
                        {
                            this.ucPageTitle1.PageName = ClsChangeRequestHelper.Delete;
                            this.updatePanelSubscriber.Visible = true;
                            this.SetNewInfoVisble(false);
                            this.SetOldInfoVisble(true);
                            this.btnFind.Visible = true;                            
                            return;
                        }
                    case ClsChangeRequestHelper.ChangeRequestType.Teminate:
                        {
                            this.ucPageTitle1.PageName = ClsChangeRequestHelper.Teminate;
                            this.updatePanelSubscriber.Visible = true;
                            this.SetNewInfoVisble(true);
                            this.SetOldInfoVisble(true);
                            this.btnFind.Visible = true;

                            if (this.ChangeRequestStatus == ClsChangeRequestHelper.ChangeRequestStatus.New)
                            {
                                this.ddlNewOrg.Enabled = false;
                                this.ddlNewSite.Enabled = false;
                                this.ddlNewCC.Enabled = false;
                                this.txtNewEmail.Enabled = false;
                                this.txtNewUsername.Enabled = false;
                                this.ddlNewServiceType.Enabled = false;
                                this.txtNewAssetID.Enabled = false;
                                //this.txtNewHostname.Enabled = false;
                                this.txtNewEffDate.Enabled = false;
                                this.txtNewTermDate.Enabled = true;
                                this.rblNewStatus.Enabled = false;
                            }
                            return;
                        }
                    case ClsChangeRequestHelper.ChangeRequestType.CostCentreTransfer:
                        {
                            this.ucPageTitle1.PageName = ClsChangeRequestHelper.CostCentreTransfer;
                            this.updatePanelSubscriber.Visible = true;
                            this.SetNewInfoVisble(true);
                            this.SetOldInfoVisble(true);
                            this.btnFind.Visible = true;

                            if (this.ChangeRequestStatus == ClsChangeRequestHelper.ChangeRequestStatus.New)
                            {
                                this.ddlNewOrg.Enabled = true;
                                this.ddlNewSite.Enabled = true;
                                this.ddlNewCC.Enabled = true;
                                this.txtNewEmail.Enabled = false;
                                this.txtNewUsername.Enabled = false;
                                this.ddlNewServiceType.Enabled = false;
                                this.txtNewAssetID.Enabled = false;
                                //this.txtNewHostname.Enabled = false;
                                this.txtNewEffDate.Enabled = true;
                                this.txtNewTermDate.Enabled = false;
                                this.rblNewStatus.Enabled = false;
                            }
                            return;
                        }
                }
            }
        }

        public ClsChangeRequestHelper.ChangeRequestStatus ChangeRequestStatus
        {
            get
            {
                return (ClsChangeRequestHelper.ChangeRequestStatus)Int64.Parse(this.ViewState["CRStatus"].ToString());
            }
            set
            {
                this.ViewState["CRStatus"] = ((Int64)value).ToString();
                switch (value)
                {
                    case ClsChangeRequestHelper.ChangeRequestStatus.Submitted:
                        {
                            ClsUtilityHelper.DisableReadOnlyFormControlValues((Control)this.updatePanelHeader);
                            ClsUtilityHelper.DisableReadOnlyFormControlValues((Control)this.updatePanelSubscriber);
                            ClsUtilityHelper.DisableReadOnlyFormControlValues((Control)this.PanelRequestor);
                            value = ClsChangeRequestHelper.ChangeRequestStatus.Submitted;
                            return;
                        }
                }
                this.lblCRStatus.Text = Microsoft.Security.Application.Encoder.HtmlEncode(value.ToString());
            }
        }

        public bool IsLoginUserCRRequestor
        {
            get
            {
                return Convert.ToBoolean(this.ViewState["IsLoginUserCRRequestor"]);
            }
            set
            {
                this.ViewState["IsLoginUserCRRequestor"] = value;
            }
        }

        public bool IsLoginUserCRApprover
        {
            get
            {
                return Convert.ToBoolean(this.ViewState["IsLoginUserCRApprover"]);
            }
            set
            {
                this.ViewState["IsLoginUserCRApprover"] = value;
            }
        }

        public bool IsLoginUserCRDoer
        {
            get
            {
                return Convert.ToBoolean(this.ViewState["IsLoginUserCRDoer"]);
            }
            set
            {
                this.ViewState["IsLoginUserCRDoer"] = value;
            }
        }

        public long LoginUserGroupID
        {
            get
            {
                return Convert.ToInt64(this.ViewState["LoginUserGroupID"]);
            }
            set
            {
                this.ViewState["LoginUserGroupID"] = value;
            }
        }

        public long CreatedByGroupID
        {
            get
            {
                return Convert.ToInt64(this.ViewState["CreatedByGroupID"]);
            }
            set
            {
                this.ViewState["CreatedByGroupID"] = value;
            }
        }

        public long ChangeRequestID
        {
            get
            {
                return Convert.ToInt64(Common.DecryptData(this.xChangeRequestID.Value));
            }
        }

        private void BindCR()
        {
            this.CreatedByGroupID = 0;
            this.dt = new ClsChangeRequest()
            {
                ID = Convert.ToInt64(Common.DecryptData(this.xChangeRequestID.Value))
            }.Select();
            if (this.dt.Rows != null && this.dt.Rows.Count > 0)
            {
                this.ChangeRequestNo = this.dt.Rows[0]["ChangeRequestNo"].ToString();
                this.ChangeRequestStatus = (ClsChangeRequestHelper.ChangeRequestStatus)(Int64)this.dt.Rows[0]["ChangeRequestStatus"];
                this.ChangeRequestType = (ClsChangeRequestHelper.ChangeRequestType)(Int64)this.dt.Rows[0]["ChangeRequestTypeID"];

                this.lblCreatedByLabel.Text = "Submitted By:";
                this.lblCreatedDateTimeLabel.Text = "Submitted Date Time:";
                this.lblCreatedBy.Text = Common.EscapeName(this.dt.Rows[0]["CreatedByName"].ToString());
                this.lblCreatedDateTime.Text = Common.EscapeDate(this.dt.Rows[0]["CreatedDateTime"].ToString());

                this.txtRequestorComment.Text = this.dt.Rows[0]["RequestorComment"].ToString();
                DataTable dataTable = new ClsUserProfile()
                {
                    ID = Convert.ToInt64(this.dt.Rows[0]["CreatedBy"].ToString())
                }.Select();
                this.CreatedByGroupID = dataTable.Rows == null || dataTable.Rows.Count <= 0 ? 0 : Convert.ToInt64(dataTable.Rows[0]["UserTypeId"].ToString());
            }
            this.BindCRDetails();
            this.BindAttachment();
        }

        private void BindCRDetails()
        {
            DataTable dataTable = new ClsDetailsInChangeRequest()
            {
                ChangeRequestID = Convert.ToInt64(Common.DecryptData(this.xChangeRequestID.Value))
            }.SelectByChangeRequestID();
            if (dataTable == null || dataTable.Rows.Count <= 0)
                return;
            {
                string xOrgID = "";
                string xSiteID = "";
                string id = dataTable.Rows[0]["New_CostCentreID"].ToString();
                if (id != "" && id != "0")
                {
                    ClsCostCentresInSite.GetOrgSiteIDByCostCentreID(Convert.ToInt64(id), ref xOrgID, ref xSiteID);
                    if (xOrgID != "" && xOrgID != "0")
                    {
                        this.NewOrganizationID = xOrgID;
                        if (xSiteID != "" && xSiteID != "0")
                        {
                            this.NewSiteID = xSiteID;
                            this.NewCostCentreID = id;
                        }
                    }
                }

                this.txtNewEffDate.Text = Microsoft.Security.Application.Encoder.HtmlEncode(!(dataTable.Rows[0]["New_EffectiveDate"].ToString() != "") ? dataTable.Rows[0]["New_EffectiveDate"].ToString() : ((DateTime)dataTable.Rows[0]["New_EffectiveDate"]).ToString("dd/MM/yyyy"));
                this.txtNewTermDate.Text = Microsoft.Security.Application.Encoder.HtmlEncode(!(dataTable.Rows[0]["New_TerminatedDate"].ToString() != "") ? dataTable.Rows[0]["New_TerminatedDate"].ToString() : ((DateTime)dataTable.Rows[0]["New_TerminatedDate"]).ToString("dd/MM/yyyy"));
                this.txtNewEmail.Text = Microsoft.Security.Application.Encoder.HtmlEncode(dataTable.Rows[0]["New_Email"].ToString());
                this.txtNewUsername.Text = Microsoft.Security.Application.Encoder.HtmlEncode(dataTable.Rows[0]["New_Username"].ToString());
                //this.ddlNewServiceType.Text = Microsoft.Security.Application.Encoder.HtmlEncode(dataTable.Rows[0]["New_ServiceType"].ToString());
                this.txtNewAssetID.Text = Microsoft.Security.Application.Encoder.HtmlEncode(dataTable.Rows[0]["New_AssetID"].ToString());
                //this.txtNewHostname.Text = Microsoft.Security.Application.Encoder.HtmlEncode(dataTable.Rows[0]["New_Hostname"].ToString());
                string status = Microsoft.Security.Application.Encoder.HtmlEncode((int)dataTable.Rows[0]["New_Status"] == 1 ? "true" : "false");
                this.rblNewStatus.SelectedIndex = this.rblNewStatus.Items.IndexOf(this.rblNewStatus.Items.FindByValue(status));
            }
            if (this.ChangeRequestType != ClsChangeRequestHelper.ChangeRequestType.Create)
            {
                string xOrgIDName = "";
                string xSiteIDName = "";
                string xCostCentreIDName = "";
                if (dataTable.Rows[0]["Old_CostCentreID"].ToString() != "")
                    ClsCostCentresInSite.GetOrgSiteCCIDNameByCostCentreID(Convert.ToInt64(dataTable.Rows[0]["Old_CostCentreID"].ToString()), ref xOrgIDName, ref xSiteIDName, ref xCostCentreIDName);
                this.lblOldOrg.Text = Microsoft.Security.Application.Encoder.HtmlEncode(xOrgIDName);
                this.lblOldSite.Text = Microsoft.Security.Application.Encoder.HtmlEncode(xSiteIDName);
                this.lblOldCC.Text = Microsoft.Security.Application.Encoder.HtmlEncode(xCostCentreIDName);
                this.lblOldEffDate.Text = Common.EscapeDate(!(dataTable.Rows[0]["Old_EffectiveDate"].ToString() != "") ? dataTable.Rows[0]["Old_EffectiveDate"].ToString() : ((DateTime)dataTable.Rows[0]["Old_EffectiveDate"]).ToString("dd/MM/yyyy"));
                this.lblOldTermDate.Text = Common.EscapeDate(!(dataTable.Rows[0]["Old_TerminatedDate"].ToString() != "") ? dataTable.Rows[0]["Old_TerminatedDate"].ToString() : ((DateTime)dataTable.Rows[0]["Old_TerminatedDate"]).ToString("dd/MM/yyyy"));
                this.lblOldEmail.Text = Common.EscapeEmail(dataTable.Rows[0]["Old_Email"].ToString());
                this.lblOldUsername.Text = Common.EscapeName(dataTable.Rows[0]["Old_Username"].ToString());
                this.lblOldServiceType.Text = Common.EscapeName(dataTable.Rows[0]["Old_ServiceType"].ToString());
                this.lblOldAssetID.Text = Common.EscapeName(dataTable.Rows[0]["Old_AssetID"].ToString());
                //this.lblOldHostname.Text = Microsoft.Security.Application.Encoder.HtmlEncode(dataTable.Rows[0]["Old_Hostname"].ToString());
                this.rblOldStatus.SelectedIndex = this.rblOldStatus.Items.IndexOf(this.rblOldStatus.Items.FindByValue((int)dataTable.Rows[0]["Old_Status"] == 1 ? "true" : "false"));
            }
            if (this.ChangeRequestType == ClsChangeRequestHelper.ChangeRequestType.CostCentreTransfer)
            {                
                this.rblOldStatus.SelectedIndex = this.rblOldStatus.Items.IndexOf(this.rblOldStatus.Items.FindByValue((int)dataTable.Rows[0]["Old_Status"] == 1 ? "true" : "false"));
            }
        }

        private void BindAttachment()
        {
            this.BindAttachmentRequestor();
        }

        private void BindAttachmentRequestor()
        {
            if (!this.Page.IsPostBack)
            {
                ClsAttachmentsInChangeRequest attachmentsInChangeRequest = new ClsAttachmentsInChangeRequest();
                attachmentsInChangeRequest.ChangeRequestID = Convert.ToInt64(Common.DecryptData(this.xChangeRequestID.Value));
                attachmentsInChangeRequest.ChangeRequestRole = 1;
                if (Common.DecryptData(this.xChangeRequestID.Value) != "0")
                    this.dtAttachmentRequestor = attachmentsInChangeRequest.SelectByChangeRequestIDRole();
                if (this.dtAttachmentRequestor != null && this.dtAttachmentRequestor.Rows.Count > 0)
                    this.ViewState["CRAttachmentRequestor"] = this.dtAttachmentRequestor;
                else if (this.ViewState["CRAttachmentRequestor"] == null)
                    this.ViewState["CRAttachmentRequestor"] = attachmentsInChangeRequest.DT;
                else
                    this.ViewState["CRAttachmentRequestor"] = this.ucCRAttachmentRequestor.DT;
            }
            else
                this.ViewState["CRAttachmentRequestor"] = this.ucCRAttachmentRequestor.DT;
            this.dtAttachmentRequestor = (DataTable)this.ViewState["CRAttachmentRequestor"];
            this.ucCRAttachmentRequestor.DT = this.dtAttachmentRequestor;
        }

        private void BindNewOrganization()
        {
            this.ddlNewOrg.DataSource = new ClsOrganization().SelectAll();
            this.ddlNewOrg.DataTextField = "OrgIDName";
            this.ddlNewOrg.DataValueField = "ID";
            this.ddlNewOrg.DataBind();
            this.BindNewSite();
        }

        public void BindNewSite()
        {
            this.ddlNewSite.Items.Clear();
            if (this.ddlNewOrg.SelectedIndex == -1)
                return;
            this.ddlNewSite.DataSource = new ClsSitesInOrganization()
            {
                OrgID = Convert.ToInt64(this.ddlNewOrg.SelectedValue)
            }.SelectByOrgID();
            this.ddlNewSite.DataTextField = "SiteIDName";
            this.ddlNewSite.DataValueField = "ID";
            this.ddlNewSite.DataBind();
            this.BindNewCostCentre();
        }

        private void BindNewCostCentre()
        {
            this.ddlNewCC.Items.Clear();
            if (this.ddlNewSite.SelectedIndex == -1)
                return;
            this.ddlNewCC.DataSource = new ClsCostCentresInSite()
            {
                SiteID = Convert.ToInt64(this.ddlNewSite.SelectedValue)
            }.SelectBySiteID();
            this.ddlNewCC.DataTextField = "CostCentreIDName";
            this.ddlNewCC.DataValueField = "ID";
            this.ddlNewCC.DataBind();
        }

        public string SaveChangeRequest(ClsChangeRequestHelper.ChangeRequestStatus CRStatus)
        {
            ClsChangeRequest clsChangeRequest = new ClsChangeRequest();
            clsChangeRequest.ChangeRequestTypeID = Convert.ToInt64(this.xChangeRequestType.Value);
            clsChangeRequest.ChangeRequestStatus = (long)ClsChangeRequestHelper.ChangeRequestStatus.Submitted;
            if (CRStatus == ClsChangeRequestHelper.ChangeRequestStatus.New)
                this.ChangeRequestNo = clsChangeRequest.GetNewCRNo((Int64)ClsChangeRequestHelper.ChangeRequestStatus.Submitted);
            clsChangeRequest.RequestorComment = this.txtRequestorComment.Text;
            clsChangeRequest.ChangeRequestNo = this.ChangeRequestNo;
            long id;
            if (Common.DecryptData(this.xChangeRequestID.Value) == "0")
            {
                clsChangeRequest.CreatedBy = LoginUser.UserID;
                id = Convert.ToInt64(clsChangeRequest.Save().ToString());
            }
            else
            {
                id = Convert.ToInt64(Common.DecryptData(this.xChangeRequestID.Value));
                clsChangeRequest.ID = id;
                clsChangeRequest.ModifiedBy = LoginUser.UserID;
                clsChangeRequest.Update();
            }

            ClsDetailsInChangeRequest details = new ClsDetailsInChangeRequest();
            details.ChangeRequestID = id;
            details.DeleteByChangeRequestID();
            details.New_CostCentreID = Convert.ToInt64(this.NewCostCentreID);
            details.New_EffectiveDate = Convert.ToDateTime(this.txtNewEffDate.Text, ClsApplicationConfiguration.CurrentCultureInfo);
            details.New_TerminatedDate = this.txtNewTermDate.Text.Trim() != "" ? Convert.ToDateTime(this.txtNewTermDate.Text, ClsApplicationConfiguration.CurrentCultureInfo) : DateTime.MinValue;
            details.New_Email = this.txtNewEmail.Text;
            details.New_Username = this.txtNewUsername.Text;
            details.New_ServiceType = this.ddlNewServiceType.Text;
            details.New_AssetID = this.txtNewAssetID.Text.Replace("?", "").Trim();
            details.New_Hostname = ""; //this.txtNewHostname.Text;
            details.New_OS = "";
            if (this.ChangeRequestType == ClsChangeRequestHelper.ChangeRequestType.Delete)
                details.New_Status = false;
            else
                details.New_Status = Convert.ToBoolean(this.rblNewStatus.SelectedValue);
            details.Save((int)this.ChangeRequestType, Convert.ToInt64(this.xSubscriberID));

            ClsAttachmentsInChangeRequest attachmentsInChangeRequest = new ClsAttachmentsInChangeRequest();
            if (this.ucCRAttachmentRequestor.DT.Rows.Count > 0)
            {
                attachmentsInChangeRequest.ChangeRequestID = id;
                attachmentsInChangeRequest.ChangeRequestRole = (long)this.ucCRAttachmentRequestor.RoleID;
                attachmentsInChangeRequest.DeleteByChangeRequestIDRole();
                foreach (DataRow dataRow in (InternalDataCollectionBase)this.ucCRAttachmentRequestor.DT.Rows)
                {
                    attachmentsInChangeRequest.AttachmentName = dataRow["AttachmentName"].ToString();
                    attachmentsInChangeRequest.AttachmentDetails = (byte[])dataRow["AttachmentDetails"];
                    attachmentsInChangeRequest.AttachmentContentType = dataRow["AttachMentContentType"].ToString();
                    attachmentsInChangeRequest.CreatedBy = LoginUser.UserID;
                    attachmentsInChangeRequest.Save();
                }
            }
            this.xChangeRequestID.Value = Common.EncryptData(id.ToString());
            return ClsMsgHelper.RecordSavedSucces;
        }

        private void SetOldInfoVisble(bool xVisible)
        {
            this.lblOldSubscriber.Visible = xVisible;
            this.lblOldOrg.Visible = xVisible;
            this.lblOldSite.Visible = xVisible;
            this.lblOldCC.Visible = xVisible;
            this.lblOldEmail.Visible = xVisible;
            this.lblOldUsername.Visible = xVisible;
            this.lblOldServiceType.Visible = xVisible;
            this.lblOldAssetID.Visible = xVisible;
            //this.lblOldHostname.Visible = xVisible;
            this.lblOldEffDate.Visible = xVisible;
            this.lblOldTermDate.Visible = xVisible;
            this.rblOldStatus.Visible = xVisible;
        }

        private void SetNewInfoVisble(bool xVisible)
        {
            this.lblNewSubscriber.Visible = xVisible;
            this.ddlNewOrg.Visible = xVisible;
            this.ddlNewSite.Visible = xVisible;
            this.ddlNewCC.Visible = xVisible;
            this.txtNewEmail.Visible = xVisible;
            this.txtNewUsername.Visible = xVisible;
            this.ddlNewServiceType.Visible = xVisible;
            this.txtNewAssetID.Visible = xVisible;
            //this.txtNewHostname.Visible = xVisible;
            this.txtNewEffDate.Visible = xVisible;
            this.txtNewTermDate.Visible = xVisible;
            this.rblNewStatus.Visible = xVisible;
        }
        
        private void SetActionButtonVisible()
        {

            this.BtnSubmit.Visible = Convert.ToInt32(this.ChangeRequestStatus) < 2;
            this.btnFind.Visible = this.btnFind.Visible && this.BtnSubmit.Visible;
            this.ucCRAttachmentRequestor.AttachmentPanel = this.BtnSubmit.Visible;
        }

        private void DisableAllucCRAttachment()
        {
            this.ucCRAttachmentRequestor.AttachmentPanel = false;
        }

        private void SetSelectedRecord(DataRow xdr)
        {
            string xOrgIDName = "";
            string xSiteIDName = "";
            string xCostCentreIDName = "";
            if (xdr["CostCentreID"].ToString() != "")
                ClsCostCentresInSite.GetOrgSiteCCIDNameByCostCentreID(Convert.ToInt64(xdr["CostCentreID"].ToString()), ref xOrgIDName, ref xSiteIDName, ref xCostCentreIDName);
            this.lblOldOrg.Text = Microsoft.Security.Application.Encoder.HtmlEncode(xOrgIDName);
            this.lblOldSite.Text = Microsoft.Security.Application.Encoder.HtmlEncode(xSiteIDName);
            this.lblOldCC.Text = Microsoft.Security.Application.Encoder.HtmlEncode(xCostCentreIDName);
            this.lblOldEmail.Text = Common.EscapeEmail(xdr["Email"].ToString());
            this.lblOldUsername.Text = Common.EscapeName(xdr["Username"].ToString());
            this.lblOldServiceType.Text = Common.EscapeName(xdr["ServiceType"].ToString());
            this.lblOldAssetID.Text = Common.EscapeName(xdr["AssetID"].ToString());
            //this.lblOldHostname.Text = Microsoft.Security.Application.Encoder.HtmlEncode(xdr["Hostname"].ToString());
            if (xdr["EffectiveDate"].ToString() != "")
                this.lblOldEffDate.Text = Common.EscapeDate(((DateTime)xdr["EffectiveDate"]).ToString("dd/MM/yyyy").Trim());
            else
                this.lblOldEffDate.Text = "";
            if (xdr["TerminatedDate"].ToString() != "")
                this.lblOldTermDate.Text = Common.EscapeDate(((DateTime)xdr["TerminatedDate"]).ToString("dd/MM/yyyy").Trim());
            else
                this.lblOldTermDate.Text = "";
            this.rblOldStatus.SelectedIndex = this.rblOldStatus.Items.IndexOf(this.rblOldStatus.Items.FindByValue((int)xdr["Status"] == 1 ? "true" : "false"));

            string xOrgID = "";
            string xSiteID = "";
            if (xdr["CostCentreID"].ToString() != "" && xdr["CostCentreID"].ToString() != "0")
            {
                ClsCostCentresInSite.GetOrgSiteIDByCostCentreID(Convert.ToInt64(xdr["CostCentreID"].ToString()), ref xOrgID, ref xSiteID);
                if (xOrgID != "" && xOrgID != "0")
                {
                    this.NewOrganizationID = xOrgID;
                    if (xSiteID != "" && xSiteID != "0")
                    {
                        this.NewSiteID = Microsoft.Security.Application.Encoder.HtmlEncode(xSiteID);
                        this.NewCostCentreID = Microsoft.Security.Application.Encoder.HtmlEncode(xdr["CostCentreID"].ToString());
                    }
                }
            }
            this.txtNewEmail.Text = Microsoft.Security.Application.Encoder.HtmlEncode(xdr["Email"].ToString());
            this.txtNewUsername.Text = Microsoft.Security.Application.Encoder.HtmlEncode(xdr["Username"].ToString());
            //this.ddlNewServiceType.Text = Microsoft.Security.Application.Encoder.HtmlEncode(xdr["ServiceType"].ToString());
            this.txtNewAssetID.Text = Microsoft.Security.Application.Encoder.HtmlEncode(xdr["AssetID"].ToString());
            //this.txtNewHostname.Text = Microsoft.Security.Application.Encoder.HtmlEncode(xdr["Hostname"].ToString());
            if (xdr["EffectiveDate"].ToString() != "")
                this.txtNewEffDate.Text = Microsoft.Security.Application.Encoder.HtmlEncode(((DateTime)xdr["EffectiveDate"]).ToString("dd/MM/yyyy"));
            else
                this.txtNewEffDate.Text = Microsoft.Security.Application.Encoder.HtmlEncode(xdr["EffectiveDate"].ToString());
            if (xdr["TerminatedDate"].ToString() != "")
                this.txtNewTermDate.Text = Microsoft.Security.Application.Encoder.HtmlEncode(((DateTime)xdr["TerminatedDate"]).ToString("dd/MM/yyyy"));
            else
                this.txtNewTermDate.Text = Microsoft.Security.Application.Encoder.HtmlEncode(xdr["TerminatedDate"].ToString());
            
            if (this.ChangeRequestType == ClsChangeRequestHelper.ChangeRequestType.Delete)
                this.rblNewStatus.SelectedIndex = this.rblNewStatus.Items.IndexOf(this.rblNewStatus.Items.FindByValue("false"));
            else
                this.rblNewStatus.SelectedIndex = this.rblNewStatus.Items.IndexOf(this.rblNewStatus.Items.FindByValue((int)xdr["Status"] == 1 ? "true" : "false"));
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.Session["SiteID"] == null)
                this.Response.Redirect("default.aspx", true);
            try
            {
                this.ucHighlight1.CloseAll();
                this.ucFinder1.DialogName = "#dialogFind";
                if (this.Page.IsPostBack)
                    return;
                this.IsLoginUserCRRequestor = ClsChangeRequestRoleGroup.IsUserCRRequestor(LoginUser.UserID);
                DataTable dataTable = new ClsUserProfile()
                {
                    ID = Convert.ToInt64(LoginUser.UserID)
                }.Select();
                this.LoginUserGroupID = dataTable.Rows == null || dataTable.Rows.Count <= 0 ? 0 : Convert.ToInt64(dataTable.Rows[0]["UserTypeId"].ToString());
                BindNewOrganization(); 
                if (this.PreviousPage is NewChangeRequest)                
                {
                    this.xChangeRequestID.Value = Common.EncryptData("0");                    
                    this.ChangeRequestStatus = ClsChangeRequestHelper.ChangeRequestStatus.New;
                    this.ChangeRequestType = (ClsChangeRequestHelper.ChangeRequestType)(int.Parse(((NewChangeRequest)this.PreviousPage).ChangeRequestType));
                    this.Create0 = ((NewChangeRequest)this.PreviousPage).Create0;
                    this.Update0 = ((NewChangeRequest)this.PreviousPage).Update0;
                    this.Delete0 = ((NewChangeRequest)this.PreviousPage).Delete0;
                    this.Read0 = ((NewChangeRequest)this.PreviousPage).Read0;
                    ChangeRequest changeRequest = this;
                    changeRequest.ChangeRequestStatus = ClsChangeRequestHelper.ChangeRequestStatus.New;
                    this.lblCreatedByLabel.Text = "Created By:";
                    this.lblCreatedDateTimeLabel.Text = "Created Date Time:";
                    this.lblCreatedBy.Text = Microsoft.Security.Application.Encoder.HtmlEncode(LoginUser.DisplayName);
                    this.lblCreatedDateTime.Text = Microsoft.Security.Application.Encoder.HtmlEncode(DateTime.Now.ToString());
                    this.CreatedByGroupID = this.LoginUserGroupID;
                }
                else if (this.PreviousPage is ChangeRequestSubmittedList)
                {
                    this.xChangeRequestID.Value = ((ChangeRequestSubmittedList)this.PreviousPage).ChangeRequestID;
                    this.Create0 = ((ChangeRequestSubmittedList)this.PreviousPage).Create0;
                    this.Update0 = ((ChangeRequestSubmittedList)this.PreviousPage).Update0;
                    this.Delete0 = ((ChangeRequestSubmittedList)this.PreviousPage).Delete0;
                    this.Read0 = ((ChangeRequestSubmittedList)this.PreviousPage).Read0;
                }
                else
                {
                    this.xChangeRequestID.Value = Common.EncryptData("0");
                    this.ucHighlight1.Message = "Record Not found";
                }
                if (Common.DecryptData(this.xChangeRequestID.Value) != "0")
                {
                    this.BindCR();
                    string changeRequestType1 = ((int)this.ChangeRequestType).ToString();
                    string changeRequestType2 = ((int)this.ChangeRequestType).ToString();
                    if (this.ChangeRequestType == ClsChangeRequestHelper.ChangeRequestType.Update)
                        this.SetChangedControlToBold();
                }
                else
                {
                    this.rblNewStatus.SelectedIndex = this.rblNewStatus.Items.IndexOf(this.rblNewStatus.Items.FindByValue("true"));
                }
                this.SetActionButtonVisible();
            }
            catch (Exception ex)
            {
                EPPLog.Logger.Error(MyLib.Utility.ExceptionInfo(ex));
            }
        }

        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            this.BindAttachment();
            if (this.ucFinder1.PrimaryKey == 0)
                return;
            try
            {
                if (this.ChangeRequestType != ClsChangeRequestHelper.ChangeRequestType.Create)
                {
                    this.xSubscriberID = this.ucFinder1.PrimaryKey.ToString();
                    this.SetSelectedRecord(this.ucFinder1.DataRowRecord);
                }
            }
            finally
            {
                this.ucFinder1.PrimaryKey = 0;
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            ViewStateUserKey = Session.SessionID;
        }
        
        protected void BtnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.NewSiteID == "0")
                {
                    this.ucHighlight1.ErrorMessage = "Please select Site";
                    return;
                }
                if (this.NewCostCentreID == "0")
                {
                    this.ucHighlight1.ErrorMessage = "Please select Cost Centre";
                    return;
                }

                if (this.ChangeRequestType == ClsChangeRequestHelper.ChangeRequestType.Create
                    || this.ChangeRequestType == ClsChangeRequestHelper.ChangeRequestType.Update)
                {
                    //if (this.txtNewEmail.Text == "")
                    //{
                    //    this.ucHighlight1.ErrorMessage = "Please enter the user Email Address";
                    //    return;
                    //}
                    //if (!ClsValidationHelper.IsEmail(this.txtNewEmail.Text))
                    //{
                    //    this.ucHighlight1.ErrorMessage = "Please enter valid Email Address";
                    //    return;
                    //}
                    if (this.txtNewUsername.Text == "")
                    {
                        this.ucHighlight1.ErrorMessage = "Please enter user name";
                        return;
                    }
                    if (this.txtNewAssetID.Text == "")
                    {
                        this.ucHighlight1.ErrorMessage = "Please enter host name";
                        return;
                    }
                    if (this.ddlNewServiceType.Text == "")
                    {
                        this.ucHighlight1.ErrorMessage = "Please enter Service Type";
                        return;
                    }
                    //if (this.txtNewHostname.Text == "")
                    //{
                    //    this.ucHighlight1.ErrorMessage = "Please enter host name";
                    //    return;
                    //}
                    if (this.txtNewEffDate.Text == "")
                    {
                        this.ucHighlight1.ErrorMessage = "Please enter the Effective Date";
                        return;
                    }

                    if (this.ChangeRequestType == ClsChangeRequestHelper.ChangeRequestType.Create)
                    {
                        ClsSubscriber Subscriber = new ClsSubscriber();
                        DataTable dt = Subscriber.CheckStartDate_MultiProvisioning(
                            this.txtNewAssetID.Text
                            , Convert.ToDateTime(this.txtNewEffDate.Text, ClsApplicationConfiguration.CurrentCultureInfo)
                            , this.txtNewTermDate.Text.Trim() != "" ? Convert.ToDateTime(this.txtNewTermDate.Text, ClsApplicationConfiguration.CurrentCultureInfo) : DateTime.MinValue
                            , 0);
                        if (dt.Rows[0][0].ToString().Trim() != "")
                        {
                            this.ucHighlight1.ErrorMessage = dt.Rows[0][0].ToString().Trim();
                            return;
                        }
                    }
                }
                else if (this.ChangeRequestType == ClsChangeRequestHelper.ChangeRequestType.Teminate)
                {
                    if (this.txtNewTermDate.Text.Trim() == "")
                    {
                        this.ucHighlight1.ErrorMessage = "Terminate Date is required!";
                        return;
                    }
                }

                if (this.ChangeRequestType == ClsChangeRequestHelper.ChangeRequestType.Update
                    || this.ChangeRequestType == ClsChangeRequestHelper.ChangeRequestType.Delete
                    || this.ChangeRequestType == ClsChangeRequestHelper.ChangeRequestType.Teminate
                    || this.ChangeRequestType == ClsChangeRequestHelper.ChangeRequestType.CostCentreTransfer)
                {
                    if (this.ChangeRequestType == ClsChangeRequestHelper.ChangeRequestType.CostCentreTransfer)
                    {
                        ClsSubscriber Subscriber = new ClsSubscriber();
                        DataTable dataTable2 = Subscriber.SelectByAssetID(this.txtNewAssetID.Text);
                        if (this.NewCostCentreID == Convert.ToString(dataTable2.Rows[0]["CostCentreID"]))
                        {
                            this.ucHighlight1.ErrorMessage = "Please select new cost centre";
                            return;
                        }

                        DataTable dt = Subscriber.CheckCCStartDate(this.txtNewAssetID.Text, Convert.ToDateTime(this.txtNewEffDate.Text, ClsApplicationConfiguration.CurrentCultureInfo));
                        if (dt.Rows[0][0].ToString().Trim() != "")
                        {
                            this.ucHighlight1.ErrorMessage = dt.Rows[0][0].ToString().Trim();
                            return;
                        }
                    }

                    if (this.xSubscriberID == "0")
                    {
                        this.ucHighlight1.ErrorMessage = "Please select a subscriber to be updated";
                        return;
                    }

                    if (this.txtNewAssetID.Text == "")
                    {
                        this.ucHighlight1.ErrorMessage = "Please enter the user Email Address";
                        return;
                    }
                    if (this.txtNewAssetID.Text != lblOldAssetID.Text)
                    {
                        if (LoginUser.GroupName.ToUpper() != "PORTAL ADMIN")
                        {
                            this.ucHighlight1.ErrorMessage = "Only 'Protal Admin' group can edit Host name";
                            return;
                        }
                    }

                    if (this.ChangeRequestType == ClsChangeRequestHelper.ChangeRequestType.Update || this.ChangeRequestType == ClsChangeRequestHelper.ChangeRequestType.Teminate)
                    {
                        ClsSubscriber Subscriber = new ClsSubscriber();
                        DataTable dt = Subscriber.CheckStartDate_MultiProvisioning(this.txtNewAssetID.Text
                            , Convert.ToDateTime(this.txtNewEffDate.Text, ClsApplicationConfiguration.CurrentCultureInfo)
                            , this.txtNewTermDate.Text.Trim() != "" ? Convert.ToDateTime(this.txtNewTermDate.Text, ClsApplicationConfiguration.CurrentCultureInfo) : DateTime.MinValue
                            , Convert.ToInt64(this.xSubscriberID));
                        if (dt.Rows[0][0].ToString().Trim() != "")
                        {
                            this.ucHighlight1.ErrorMessage = dt.Rows[0][0].ToString().Trim();
                            return;
                        }
                    }
                }
                
                DateTime EffectiveDate = Convert.ToDateTime(this.txtNewEffDate.Text, ClsApplicationConfiguration.CurrentCultureInfo);
                DateTime TerminatedDate = this.txtNewTermDate.Text.Trim() != "" ? Convert.ToDateTime(this.txtNewTermDate.Text, ClsApplicationConfiguration.CurrentCultureInfo) : DateTime.MaxValue;
                if (EffectiveDate >= TerminatedDate)
                {
                    this.ucHighlight1.ErrorMessage = "TerminatedDate should great than EffectiveDate";
                    return;
                }
                
                this.SaveChangeRequest(this.ChangeRequestStatus);
                ClsUtilityHelper.HideAllButtonControl((Control)this);
                this.DisableAllucCRAttachment();
                this.ucHighlight1.Message = "Request has been submitted.";
                ChangeRequest changeRequest = this;
                changeRequest.ChangeRequestStatus = ClsChangeRequestHelper.ChangeRequestStatus.Submitted;
            }
            catch (Exception ex)
            {
                EPPLog.Logger.Error("Exception:\r\n Type:" + ex.GetType() + ";\r\n Message:" + ex.Message + ";\r\n Trace:" + ex.StackTrace);
            }
        }     

        protected void ddlNewOrg_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.BindNewSite();
        }

        protected void ddlNewSite_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.BindNewCostCentre();
        }

        protected void btnFind_Click(object sender, EventArgs e)
        {
            this.ucFinder1.OrgID = LoginUser.OrganizationID;
            this.ucFinder1.PrepareData();
            ClsUtilityHelper.OpenJQueryDialog(this.ucFinder1.DialogName, (Control)this);
        }

        protected void SetControlAToBoldIfControlBValueDifferFromControlC(Label LabelA, TextBox TextBoxB, Label LabelC)
        {
            if (!(TextBoxB.Text != LabelC.Text))
                return;
            LabelA.Font.Bold = true;
        }

        protected void SetControlAToBoldIfControlBValueDifferFromControlC(Label LabelA, DropDownList DropDownListB, Label LabelC)
        {
            if (!(DropDownListB.SelectedItem.Text != LabelC.Text))
                return;
            LabelA.Font.Bold = true;
        }

        protected void SetControlAToBoldIfControlBValueDifferFromControlC(Label LabelA, RadioButtonList RadioButtonListB, RadioButtonList RadioButtonListC)
        {
            if (!(RadioButtonListB.SelectedValue != RadioButtonListC.SelectedValue))
                return;
            LabelA.Font.Bold = true;
        }

        protected void SetControlAToBoldIfControlBValueDifferFromControlC(Label LabelA, DropDownList DropDownListB, DropDownList DropDownListC)
        {
            if (!(DropDownListB.Text != DropDownListC.Text))
                return;
            LabelA.Font.Bold = true;
        }

        protected void SetChangedControlToBold()
        {
            this.SetControlAToBoldIfControlBValueDifferFromControlC(this.lblOrg, this.ddlNewOrg, this.lblOldOrg);
            this.SetControlAToBoldIfControlBValueDifferFromControlC(this.lblSite, this.ddlNewSite, this.lblOldSite);
            this.SetControlAToBoldIfControlBValueDifferFromControlC(this.lblCC, this.ddlNewCC, this.lblOldCC);
            this.SetControlAToBoldIfControlBValueDifferFromControlC(this.lblEmail, this.txtNewEmail, this.lblOldEmail);
            this.SetControlAToBoldIfControlBValueDifferFromControlC(this.lblUsername, this.txtNewUsername, this.lblOldUsername);
            this.SetControlAToBoldIfControlBValueDifferFromControlC(this.lblServiceType, this.ddlNewServiceType, this.lblOldServiceType);
            this.SetControlAToBoldIfControlBValueDifferFromControlC(this.lblAssetID, this.txtNewAssetID, this.lblOldAssetID);
            //this.SetControlAToBoldIfControlBValueDifferFromControlC(this.lblHostname, this.txtNewHostname, this.lblOldHostname);
            this.SetControlAToBoldIfControlBValueDifferFromControlC(this.lblEffDate, this.txtNewEffDate, this.lblOldEffDate);
            this.SetControlAToBoldIfControlBValueDifferFromControlC(this.lblTermDate, this.txtNewTermDate, this.lblOldTermDate);
            this.SetControlAToBoldIfControlBValueDifferFromControlC(this.lblStatus, this.rblNewStatus, this.rblOldStatus);
        }

        protected void BtnPrint_Click(object sender, EventArgs e)
        {
            //this.Server.Transfer("CRReportForm.aspx");
        }

    }
}
